package kite.sso.service.impl;

import com.tfrunning.toolkit.StrKit;
import kite.sso.message.Response;
import kite.sso.model.KiteSysdef;
import kite.sso.service.KiteSysService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: <br>
 * 所属包名: kite.sso.service.impl<br>
 * 创建人　: 白剑<br>
 * 创建时间: 06/07/2018 09:53 Thursday<br>
 * 当前版本: 1.0<br>
 * 修改历史: <br>
 * 修改时间　　　　　修改人　　　　　版本变更　　　　　修改说明<br>
 * -----------------------------------------------------<br>
 * <br>
 */
@Service
@Transactional
public class KiteSysServiceImpl implements KiteSysService {

    @Autowired
    private Dao dao;

    @Override
    @Cacheable("allSysList")
    public String getUserSys(String userId) {

        Response response = new Response();

        try {
            Sql sql = Sqls.create("select s1.* from kite_sysdef s1 where s1.sys_id in "
                    + "(select s2.sys_id from kite_usersys s2 where s2.user_id = '$userId') and s1.sys_isenable = 'Y' order by s1.sys_id");
            sql.vars().set("userId", userId);
            sql.setCallback(Sqls.callback.entities());
            sql.setEntity(dao.getEntity(KiteSysdef.class));
            dao.execute(sql);
            List<KiteSysdef> usersysList = sql.getList(KiteSysdef.class);

            StringBuilder sysIds = new StringBuilder();
            for (KiteSysdef s : usersysList) {
                sysIds.append("'").append(s.getSysId()).append("',");
            }
            sysIds = new StringBuilder(StrKit.toUncommaStr(sysIds.toString(), ","));

            List<KiteSysdef> othersysList = new ArrayList<>();
            if (StrKit.isBlank(sysIds.toString())) {
                othersysList = dao.query(KiteSysdef.class, null);
            } else {
                Criteria cri = Cnd.cri();
                cri.where().and("sysId", "not in", sysIds.toString());
                cri.where().andEquals("sysIsenable", "Y");
                cri.getOrderBy().asc("sysId");
                othersysList = dao.query(KiteSysdef.class, cri);
            }

            response.setSuccess(true);
            response.setVo(usersysList);
            response.setVo1(othersysList);

        } catch (Exception e) {
            response.setSuccess(false);
            response.setMsg("获取系统列表失败，" + e.getMessage());
        }

        return response.responseVal();
    }

    @Override
    @Cacheable("accessSysList")
    public String getUserAccessSys(String userId) {

        Sql sql = Sqls.create("select s1.* from kite_sysdef s1 where s1.sys_id in "
                + "(select s2.sys_id from kite_usersys s2 where s2.user_id = '$userId') and s1.sys_isenable = 'Y' order by s1.sys_id");
        sql.vars().set("userId", userId);
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(KiteSysdef.class));
        dao.execute(sql);
        List<KiteSysdef> usersysList = sql.getList(KiteSysdef.class);

        StringBuilder userAccessSys = new StringBuilder();
        for (KiteSysdef sysdef : usersysList) {
            String address = sysdef.getSysAddress();
            if (!StrKit.isBlank(address)) {
                address = address.replace("http://", "");
                userAccessSys.append(address, 0, address.indexOf("/")).append("|");
            }
        }

        return userAccessSys.length() != 0 ? userAccessSys.substring(0, userAccessSys.length() - 1) : "";
    }
}
